﻿namespace ImapComponent.Net.Mail.Utils
{
    using System;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Extension for string[].
    /// </summary>
    internal static class StringExtension
    {
        /// <summary>
        /// Returns a value indicating whether specified string object occurs in string[].
        /// </summary>
        /// <param name="stringArray">String array.</param>
        /// <param name="value">The string to seek.</param>
        /// <returns>true if the value parameter occurs within string[]; otherwise, false</returns>
        internal static bool Contains(this string[] stringArray, string value)
        {
            foreach (string str in stringArray)
            {
                if (str.IndexOf(value) != -1)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Returns a string value which contains specified pattern.
        /// </summary>
        /// <param name="stringArray">String array.</param>
        /// <param name="pattern">The string pattern to seek.</param>
        /// <returns>A string value which contains pattern or empty string.</returns>
        internal static string GetStringContain(this string[] stringArray, string pattern)
        {
            foreach (string str in stringArray)
            {
                if (str.IndexOf(pattern) != -1)
                {
                    return str;
                }
            }

            return string.Empty;
        }

        /// <summary>
        /// Gets a number of occurences the specified character in the string.
        /// </summary>
        /// <param name="str">A string in which is the character searched.</param>
        /// <param name="character">A character to search.</param>
        /// <returns>Number of occurences the specified character in the string.</returns>
        internal static int GetOccurenceCount(this string str, char character)
        {
            int count = 0;

            foreach (char chr in str)
            {
                if (chr == character)
                {
                    count++;
                }
            }

            return count;
        }

        /// <summary>
        /// Gets the string in brackets.
        /// </summary>
        /// <param name="data">The string data.</param>
        /// <returns>The string data in brackets.</returns>
        internal static string GetStringWithinBrackets(this string data)
        {
            if (data.StartsWith("("))
            {
                StringReader reader = new StringReader(data);
                StringBuilder builder = new StringBuilder();

                char character = (char)reader.Read();
                int position = 1;
                int nesting = 0;

                while (position <= data.Length)
                {
                    if (character == '(')
                    {
                        nesting++;
                    }
                    else if (character == ')')
                    {
                        nesting--;
                    }

                    builder.Append(character);

                    if (nesting == 0)
                    {
                        break;
                    }

                    character = (char)reader.Read();
                    position++;
                }

                int length = builder.ToString().Length;

                return builder.ToString().Substring(1, length - 2);
            }
            else 
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Gets the quoted string from the beggining of the data.
        /// </summary>
        /// <param name="data">The string data.</param>
        /// <returns>The quoted string.</returns>
        internal static string GetQuotedString(this string data)
        {
            if (data.StartsWith("\""))
            {
                StringReader reader = new StringReader(data);
                StringBuilder builder = new StringBuilder();

                /// skip "
                char character = (char)reader.Read();
                int position = 1;

                character = (char)reader.Read();
                position++;

                while (position <= data.Length)
                {
                    if (character == '\\')
                    {
                        builder.Append(character);
                        character = (char)reader.Read();
                        position++;
                    }
                    else if (character == '"')
                    {
                        break;
                    }

                    builder.Append(character);
                    character = (char)reader.Read();
                    position++;
                }

                int length = builder.ToString().Length;

                return builder.ToString();
            }
            else 
            {
                throw new FormatException("String does not start with \" character.");
            }
        }
    }
}
